<?php
namespace App\Controller\App;

use App\Query\UserQuery;
use App\Query\UserAddressQuery;
use App\Query\MallOrderQuery;
use App\Repository\MallGoodsRepository;
use App\Repository\MallOrderRepository;

use App\Facade\Pay;
use App\Facade\Pays;

use App\Service\Code\CodeService;

class MallOrderController extends Controller
{
    // 创建订单
    public function index($id)
    {
        $user = $this->auth();
        
        // 获取商品
        if(!$goods = MallGoodsRepository::find($id))
        {
            $this->error('error');
        }
        
        // 状态
        if(!$goods->statusOk())
        {
            $this->error('goods_status');
        }
        
        // 获取库存
        $count = abs($this->input('count'));
        
        if(!$goods->hasCount($count))
        {
            $this->error('goods_count');
        }
        
        // 限制时间数量
        if($dayCount = $goods->isDay())
        {
            // 查询订单
            $dayCount = MallOrderQuery::dayCount(MallOrderQuery::user($user), $dayCount);
            
            if($goods->isDayCount($dayCount + $count))
            {
                $this->error('day_count');
            }
        }
        
        // 收货地址
        if(!$address = UserAddressQuery::user($user)->first())
        {
            $this->error('address');
        }
        
        // 验证金额
        $money_pay = $goods->moneyPay($count);
        $money_shop = $goods->moneyShop($count);
        $money_point = $goods->moneyPoint($count);
        $money_express = $goods->moneyExpress($count);
        $money_settle = $goods->moneySettle($count);

        // 验证用户余额
        if(!$user->hasMoney('money_shop', $money_shop))
        {
            $this->error('money_shop');
        }
        
        if(!$user->hasMoney('money_point', $money_point))
        {
            $this->error('money_point');
        }
        
        // 扣除
        $user->makeMoney('money_shop', $money_shop, '购买【' . $goods->name . '】');
        $user->makeMoney('money_point', $money_point, '购买【' . $goods->name . '】');
        
        // 扣除库存
        $goods->delCount($count);

        // 创建订单
        $order = MallOrderRepository::make();
        $res = $order->create($user, $goods, [
            'name'    => $address->name,
            'phone'   => $address->phone,
            'data'    => $address->data,
            'address' => $address->address,
            'count'   => $count,
            'money_pay'     => $money_pay,
            'money_shop'    => $money_shop,
            'money_point'   => $money_point,
            'money_express' => $money_express,
            'money_settle'  => $money_settle
        ]);
        
        if(!$res) $this->error('create');
        
        // 判断是否需要在线支付
        if($order->payMoney() == 0)
        {
            $order->setStatusPay();
        }

        return ['id' => $order->id];
    }
    
    // 获取订单
    protected function find($id)
    {
        // 获取订单
        if(!$order = MallOrderQuery::user($this->auth())->find($id))
        {
            $this->error('error');
        }
        
        return MallOrderRepository::make($order);
    }
    
    // 我的订单
    public function user()
    {
        return MallOrderQuery::query(
            MallOrderQuery::user($this->auth()),
            $this->inputs('status')
        )->paginate(10)->toArray();
    }
    
    // 关闭订单
    public function no($id)
    {
        $order = $this->find($id);
        
        // 状态
        if(!$order->setStatusNo())
        {
            $this->error('order_status');
        }
        
        $this->message('ok');
    }
    
    // 确认订单
    public function ok($id)
    {
        $order = $this->find($id);
        
        // 状态
        if(!$order->setStatusOk())
        {
            $this->error('order_status');
        }
        
        $this->message('ok');
    }
    
    // 支付宝支付
    public function alipay($id)
    {
        $order = $this->find($id);

        // 状态
        if($order->statusPay())
        {
            $this->error('order_pay');
        }
        
        $response = Pays::alipay()->app()->service()
                        ->setMoney($order->payMoney())
                        ->setTitle($order->goods_name)
                        ->setOrderId($order->id)
                        ->setNotify(dirname($this->request->url()) . '/notify/' . $id)
                        ->send();
        
        if(!$response)
        {
            $this->error('error');
        }
        
        return compact('response');
    }
    
    // 支付宝回调
    public function alipayNotify($id)
    {
        // 回调 
        $notify = Pays::alipay()->notify($this->request->all());
        
        // 不能出错
        if(!$notify || $notify->error())
        {
            return;
        }
        
        if($order = MallOrderRepository::find($id))
        {
            $order->setStatusPay('alipay');
        }
        
        return $notify->response();
    }
    
    // 余额支付
    public function money($id)
    {
        $user = $this->auth();
        
        $order = $this->find($id);
        
        // 状态
        if($order->statusPay())
        {
            $this->error('order_pay');
        }
        
        // // 验证验证码
        // if(!(new CodeService($user->phone))->check($this->input('code')))
        // {
        //     $this->error('code');
        // }
        
        // 扣掉余额
        if(!$user->makeMoney('money', $order->payMoney(), '支付【' . $order->goods_name . '】'))
        {
            $this->error('money');
        }
        
        if($order->setStatusPay('money'))
        {
            $this->message('ok');
        }
        
        $this->error('error');
    }
}
